var UsersManaged = function () {
	var table;
    return {
    	init:function(){
    		
    		 if (!jQuery().DataTable) {
                 return;
             }
    		 var thisObj = this;
             // begin first table
    		 table= $('#users_table').DataTable({
	             ajax: {
	             		url:"/admin/system/user/list",
	             		data:function(d){
	             			$.extend(d,$("#searchForm").serializeJSON());
	             	    }
	             	},
	             scrollCollapse:true,
                 columns: [
                   {data:null},
                   {data:"userName"},
                   {data:"name"},
                   {data:null},
                   {data:null},
                   {data:null},
                   {data:null},
                   {data:null,
                	render:function ( data, type, full, meta ) { 
	                	 var str =  "<div class='visible-md visible-lg hidden-sm hidden-xs btn-group'>"+
																	"<button class='btn btn-xs btn-primary' name='updateUserBtn' userId='"+data.id+"' authCode='39'>"+
																	"	<i class='icon-edit'>修改</i>"+
																	"</button>";
	                	 if(data.status=='n'){
		                	 str+= "<button class='btn btn-xs btn-success' name='enableUserBtn' userId='"+data.id+"' status='n' authCode='40'>"+
										"	<i class='icon-eye-open'>启用</i>"+
									"</button>";
	                	 }else{
	                		 str+="<button class='btn btn-xs btn-pink' name='disableUserBtn' userId='"+data.id+"' status='y' authCode='40'>"+
								"	<i class='icon-eye-open'>禁用</i>"+
								"</button>";
	                	 }
	                	 str+"</div>"
						 return str;
                	 }
                   }
                 ],
                 drawCallback: function(){
	                	var api = this.api();
	                	var startIndex= api.context[0]._iDisplayStart;//获取到本页开始的条数
	                	api.column(0).nodes().each(function(cell, i) {
	                	cell.innerHTML = startIndex + i + 1;
                	}); 
                }
             });
    		 $("#search").click(function(){
    			 table.draw();
    		 })
    		 $('#addUser_dialog').on('shown.bs.modal', function () {
				var $b = $("#brandList").chosen();
			});
    		 $("#users_table").on("click","button[name='updateUserBtn']",function(){
    			 var id = $(this).attr("userId");
    			 $("#addUser_dialog").model({
     				remote:"system/toModifySysUser?id="+id	 
     			 });
    		 }).on("click","button[name='enableUserBtn']",function(){
    			 thisObj.updateUserStatus('y',$(this).attr("userId"));
    		 }).on("click","button[name='disableUserBtn']",function(){
    			 thisObj.updateUserStatus('n',$(this).attr("userId"));
    		 });
    		 
    		 $("#addUserBtn").click(function(){
    			 $("#addUser_dialog").modal({
    				remote:"system/toAddSysUser"	 
    			 });
    		 });
    		  $("#addUser_dialog").on("hidden.bs.modal", function() {
                $(this).removeData("bs.modal");
    		  }).on("click","#addUserSubmitBtn",function(){
    			  var valid=$("#addUserForm").valid();
    			  if(!valid){
    				  return ;
    			  }
    			  $("#addUser_dialog input[name='brandList']").val($("#addUser_dialog #brandList").val());
    			 thisObj.addUser();
    		 });
    	},addUser:function(){
    		 var id = $("#addUserForm input[name='id']").val();
        	 var url = "system/addSysUser";
        	 if(id!=""){
        		 url = "system/updateSysUser";
        	 }
        	 $.ajax({
                 type: "post",
                 url: url,
                 data: $("#addUserForm").serializeJSON(),
                 dataType: "json",
                 success: function(data){
                 	if(data.code==0){
                 		table.ajax.reload(null,false);
                 		$("#addUser_dialog").modal("hide");
                 	}else{
                 		alert("添加或者修改系统用户错误");
                 	}
                 }
         	});
    	},updateUserStatus:function(status,id){
    		 $.ajax({
                 type: "post",
                 url: "system/updateSysUserStatus",
                 data: "status="+status+"&id="+id,
                 dataType: "json",
                 success: function(data){
                 	if(data.code==0){
                 		table.ajax.reload(null,false);
                 		$("#addUser_dialog").modal("hide");
                 	}else{
                 		alert("添加或者修改系统用户错误");
                 	}
                 }
    		 });
    	}
    }
}();